package com.qujia.label.controller;

import com.qujia.common.annotation.Log;
import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.enums.BusinessType;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.poi.ExcelUtil;
import com.qujia.label.domain.*;
import com.qujia.label.service.ICustomerLabelDemoHistoryService;
import com.qujia.label.service.ICustomerLabelDemoService;
import com.qujia.label.service.ICustomerLabelService;
import com.qujia.label.service.ILabelRuleService;
import com.qujia.system.service.IAppUserInfoUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户标签Controller
 *
 * @author qujia
 * @date 2021-06-15
 */
@RestController
@RequestMapping("/label/labelHistory")
@Api("LabelHistoryController")
public class CustomerLabelHistoryController extends BaseController
{

    @Autowired
    private ICustomerLabelDemoHistoryService customerLabelDemoHistoryService;
    @Autowired
    private ILabelRuleService labelRuleService;


    /**
     * 查询标签用户
     */
    @GetMapping(value = "/selectCustomerLabelInfoList")
    @ApiOperation("查询标签用户")
    public TableDataInfo selectCustomerLabelInfoList(CustomerLabelDemo customerLabelDemo)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysUser user = currentUser.getUser();
        if ("7".equals(user.getUserLevel())){
            List<Long> enterpriseIds =   labelRuleService.selectEnterPriseIdList(user.getUserId());
            if (enterpriseIds != null && enterpriseIds.size()>0) {

                customerLabelDemo.setEnterpriseIdListForAuthor(enterpriseIds);

            } else {
                List<LabelRule> list = new ArrayList<>();
                return getDataTable(list);
            }
        }else{
            //customerLabelDemo.setStationId(currentUser.getUser().getDeptId());
            //customerLabelDemo.setStationIdList(sysStationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId()));
            customerLabelDemo.setEnterpriseId(currentUser.getUser().getEnterpriseId());

        }
            startPage();
        List<CustomerLabelDemo> list = new ArrayList<>();

         if(customerLabelDemo.getSecondFlag()!=null && customerLabelDemo.getSecondFlag().equals("1")) {
             if(customerLabelDemo.getEndCountNum()!=null&& customerLabelDemo.getEndCountNum()==0){
                 list=customerLabelDemoHistoryService.selectNewAppUser(customerLabelDemo);
             }else {
                 //开启二次查询
                 list = customerLabelDemoHistoryService.secondCustomerLabelDemoList(customerLabelDemo);
             }
        }else if(customerLabelDemo.getSecondFlag()!=null && customerLabelDemo.getSecondFlag().equals("0")) {
              if(customerLabelDemo.getEndCountNum()!=null&& customerLabelDemo.getEndCountNum()==0){
                  list=customerLabelDemoHistoryService.selectNewAppUser(customerLabelDemo);
              }else {
                  list = customerLabelDemoHistoryService.selectCustomerLabelDemoList(customerLabelDemo);
              }
         }

        return getDataTable(list);
    }


    /**
     * 导出用户标签列表
     */
    @Log(title = "用户标签", businessType = BusinessType.EXPORT)
    @GetMapping("/selectCustomerLabelInfoListExport")
    @ApiOperation("导出用户标签列表")
    public AjaxResult selectCustomerLabelInfoListExport(CustomerLabelDemo customerLabelDemo)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        //customerLabelDemo.setStationId(currentUser.getUser().getDeptId());
        //customerLabelDemo.setStationIdList(sysStationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId()));
        customerLabelDemo.setEnterpriseId(currentUser.getUser().getEnterpriseId());
//        customerLabelDemo.setPageSizeLimit(1000000);
        List<CustomerLabelDemo> list = new ArrayList<>();

        if(customerLabelDemo.getSecondFlag()!=null && customerLabelDemo.getSecondFlag().equals("1")) {
            if(customerLabelDemo.getEndCountNum()!=null&& customerLabelDemo.getEndCountNum()==0){
                list=customerLabelDemoHistoryService.selectNewAppUser(customerLabelDemo);
            }else {
                //开启二次查询
                list = customerLabelDemoHistoryService.secondCustomerLabelDemoList(customerLabelDemo);
            }
        }else if(customerLabelDemo.getSecondFlag()!=null && customerLabelDemo.getSecondFlag().equals("0")) {
            if(customerLabelDemo.getEndCountNum()!=null&& customerLabelDemo.getEndCountNum()==0){
                list=customerLabelDemoHistoryService.selectNewAppUser(customerLabelDemo);
            }else {
                list = customerLabelDemoHistoryService.selectCustomerLabelDemoList(customerLabelDemo);
            }
        }

        ExcelUtil<CustomerLabelDemo> util = new ExcelUtil<>(CustomerLabelDemo.class);
        return util.exportExcel(list, "label");
    }


}
